package de.koller.aframe.util.math;

import de.koller.oceanFramework.math.RVec2;

public class FunctionCutView {

	public static IFunction cutView2D( IFunction2D function, float sx, float sy, float ex, float ey ) {
		return new CutView2D( function, sx, sy, ex, ey );
	}
	
	public static IFunction2D cutView3D( IFunction3D function, float zCut ) {
		return new CutView3D( function, zCut );
	}
	
	private static class CutView2D implements IFunction {

		private final IFunction2D function;
		private final RVec2 v0, v1;

		private CutView2D( IFunction2D function, float sx, float sy, float ex, float ey ) {
			this.function = function;
			this.v0 = new RVec2( sx, sy );
			this.v1 = new RVec2( ex, ey );
		}

		@Override
		public float getValue( float x ) {
			if( 0 > x ) return 0;
			if( RVec2.difference( v0, v1 ).getLength() < x ) return 0;
			
			RVec2 v = RVec2.difference( v1, v0 );
			v.setLength( x ).add( v0 );
			return function.getValue( v.x, v.y );
		}
		
	}
	
	private static class CutView3D implements IFunction2D {

		private final IFunction3D function;
		private final float zCut;

		private CutView3D( IFunction3D function, float zCut ) {
			this.function = function;
			this.zCut = zCut;
		}

		@Override
		public float getValue( float x, float y ) {
			return function.getValue( x, y, zCut );
		}
		
	}
	
}
